15. Accessor Functions
Accessors
Accessor Functions
Accessor functions are public member functions that allow users to access an object's data, albeit indirectly.
const
Accessors should only retrieve data. They should not change the data stored in the object.
The main role of the const
specifier in accessor methods is to protect member data. When you specify a member function as const
, the compiler will prohibit that function from changing any of the object's member data.
Exercise: Bank Account Class
Your task is to design and implement class called BankAccount
. This will be a generic account defined by its account number, the name of the owner and the funds available.
Complete the following steps:
- Create class called
BankAccount
- Use typical info about bank accounts to design attributes, such as the account number, the owner name, and the available funds.
- Specify access so that member data are protected from other parts of the program.
- Create accessor and mutator functions for member data.
Workspace
This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity, so you may be able to download them there.
Workspace Information:
- Default file path:
- Workspace type: jupyter
- Opened files (when workspace is loaded): n/a